package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.UserDao;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {
  @Autowired
    private UserDao userDao;
    @Override
    public User selectUser(User user) {
        return userDao.selectUser(user);
    }

  @Override
  @DeleteCacheAnnotation
  public Map insertUser(User user) {
      Map map=new HashMap();
        int count = userDao.insertUser(user);
      if(count>0){
          map.put("isInsert",true);
      }else {
          map.put("isInsert",false);
      }
      return map;
  }

  @Override
  @DeleteCacheAnnotation
  public Map deleteUser(int uid) {
    Map map=new HashMap();
      int count = userDao.deleteUser(uid);
    if(count >0){
        map.put("isDelete",true);
    }else {
        map.put("isDelete",false);
    }
      return map;
  }

  @Override
  @DeleteCacheAnnotation
  public Map updateUser(User user) {
    Map map=new HashMap();
      int count = userDao.updateUser(user);
      if(count>0){
          map.put("isUpdate",true);
      }else {

         map.put("isUpdate",false);
      }
      return map;
  }

    @Override
    @AddCacheAnnotation
    public Map selectByPageUser(int page, int limit) {
        int offset =(page-1)*limit;
        List<User> users = userDao.selectByPageUser(offset, limit);
        int count = userDao.selectTotalCountUser();
        Map map=new HashMap();
       map.put("data",users);
       map.put("count",count);
       map.put("code",0);
       map.put("msg","");
        return map;
    }

    @Override
    public User selectOneUser(int id) {
        return userDao.selectOneUser(id);
    }

    @Override
    @AddCacheAnnotation
    public Set<String> selectRelo(String username) {
        return userDao.selectRole(username);
    }

    @Override
    @AddCacheAnnotation
    public Set<String> selectResource(String username) {
        return userDao.selectResource(username);
    }

    @Override
    public User loginUser(String username) {
        return userDao.loginUser(username);
    }

    @Override
    @AddCacheAnnotation
    public Set<String> selectRoleTest(String username) {

        User user = userDao.loginUser(username);
        if(user != null){
            return userDao.selectRoleTest(user.getUserId());
        }
        return null;
    }

    @Override
    @AddCacheAnnotation
    public Set<String> selectResourceTest(String username) {
        User user = userDao.loginUser(username);
        if (user != null) {
            Set<String> strings = userDao.selectRoleTest(user.getUserId());
            for (String string : strings) {
                return userDao.selectResourceTest(string);
            }
        }

        return null;
    }
}
